﻿<?php

FLEA::loadClass('Common_Model');

class Model_Roles extends Common_Model {

	private $_ui = null;
	
	function __construct() {
		parent::Common_Model();
		$this->_tb = FLEA::getSingleton('Table_Roles');
	}

	function getAllRoles() {
		$viewData['data'] = $this->renderList($this->_tb->findAll());
		//dump($viewData);
		//return $this->renderList($data);
		
		//$ui = FLEA::initWebControls();
		//$viewData['pager'] = $ui->control('pagernav', 'pagenav',
		//	array('pager'=>$viewData['pagerData'],'controller'=>'adminroles', 'action'=>'index' ),
		//	true
		//);
		return $viewData;
	}
	
	function getRoleById($roleId) {
		return $this->_tb->findByField ('role_id', $roleId);
	}

	function insertRole($row) {
		$row['rolename'] = strtoupper($row['rolename']);
		
		//validate is rolename existed
		//晕死，这个框架没有根据字段检验记录是否存在的
		$this->existsRecord('rolename', $row['rolename'], '该角色已存在，请更换名称！', url('adminRoles', 'insertRole'));
		return $this->_tb->create($row);
	}
	
	function updateRole($row) {
		//还没有做的：当角色的名称改变时，与roles表关联的一些数据要改变！
		$row['rolename'] = strtoupper($row['rolename']);
		$this->existsRecord('rolename', $row['rolename'], '该角色已存在，请更换名称！', url('adminRoles'));
		return $this->_tb->update($row);
	}
	
	function deleteRoleById($roleId) {
		//当删除role时，roles_users这个表里的数据要相应地删除，此时要设好表关联的enableLinks
		return $this->_tb->removeByPkv($roleId);
	}
	
	function deleteAllRoles() {
		//forbidden
	}
	
	//用PEAR的HTML_Table类生成表格，返回的是表格的HTML代码
	//要写一个方法，render公共模板，表格要改变时再override就可以了
	
	function renderList($listData) {
		//require_once 'HTML/Table.php';
		$tableFacade = FLEA::getSingleton('Common_TableFacade');
		//set table attributes
		$attributes = array("width"=>"100%", "border"=>"0");

		//create the table class
		$table = new $tableFacade($attributes);
		
		$table->setCaption('角色列表&nbsp;&nbsp;&nbsp;&nbsp;<a href=' . url('adminRoles', 'insertRole') . '>添加新角色</a>');
		//$table->setRowCount(10);
		
		//build our first row
		$contents = array("ID", "NAME", "OPERATION", "CREATED");
		$attributes = array('bgcolor'=>"#336699");
		$table->addRow($contents, $attributes, "TH");
		
		//loop through and add our data
		$attributes = array('bgcolor'=>"#COCOCO");

		foreach ($listData as $data) {
			$strOperation = '<a href=\'' . url ( 'AdminRoles', 'modifyRole', array ('role' => $data ['role_id'] ) ) . '\'>修改</a>&nbsp;&nbsp;<a href=\'' . url ( 'AdminRoles', 'deleteRole', array ('role' => $data ['role_id'] ) ) . '\'>删除</a>';
			$contents = array($data['role_id'], $data['rolename'], $strOperation, $data['created']);
			$table->addRow($contents, $attributes);
		}

		return $table->toHTML();
	}
	
	//生成角色添加和修改的表单元素
	function getForm($data) {
		$this->_ui = FLEA::initWebControls();
		$form = array(
			'role_id' => $this->_ui->control('Hidden', 'role_id',
				array(
					'value' => $data['role_id']
				),
				TRUE 
		),
			'rolename' => $this->_ui->control('Textbox', 'rolename',
				array(
					'value' => $data['rolename'],
					'maxlength' => 18
				),
				TRUE
			)
		);
		return $form;
	}

}
